<?php
/**
 * ErrorController.php
 *
 * @category   Madoqua
 * @package    Module
 * @subpackage Error
 */

/** controller base class */
require_once 'Madoqua/Controller/Action/Abstract.php';

/**
 * Error_ErrorController
 *
 * A controller to handle MVC exceptions
 *
 *
 * @category   Madoqua
 * @package    Module
 * @subpackage Error
 */
class Error_ErrorController extends Madoqua_Controller_Action_Abstract {

    /**
     * init hook
     */
    public function init() {
        parent::init();

        //$this->getHelper('layout')->setLayout('error/error');
        $this->getResponse()->clearBody();
    }

    /**
     * error action, has a look at the exception encountered and displays an
     * error page
     *
     * @todo add logging capabilities
     * @action
     */
    public function errorAction() {
        $error = $this->_getParam('error_handler');
        //the exception we've encountered

        // Push error to view
        if (!isset($this->view->errors)) {
            $errors = array();
            $this->view->errors = $errors;
        }
        $this->view->errors[] = $error['exception'];        
        

        // render 404 if no action/controller, otherwise render error page
        switch ($error->type) {
            
            // invalid or no route specified
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                 
                 $this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
                 $this->render('404');
                 break;

            // application error
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER:
                
                 $this->getResponse()->setHttpResponseCode(500);
                 
                 // Notify exception handlers
                 if($error['exception'] instanceof Madoqua_Exception) {
                    $error['exception']->handle();   
                 }
                 
                 $this->render('error');
                 break;
                 
            default:
                 
                 $this->getResponse()->setHttpResponseCode(500);
                 $this->render('error');
                 break;
        }        
    }

    /**
     * no permission acction
     *
     * @todo headers?
     * @action
     */
    public function permissionAction() {
        //no permission
    }
}